package com.zym.init.model.po;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.zym.init.core.utils.SpringUtils;
import com.zym.init.enums.ResourceTypeEnum;
import com.zym.init.model.vo.ResourceVo;
import com.zym.init.service.ResourceService;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;
import java.util.Objects;

/**
 * 资源表
 * @TableName resource
 */
@TableName(value ="resource")
@Data
public class ResourcePo implements Serializable {
    /**
     * ID
     */
    @TableId(type = IdType.AUTO)
    private Long resourceId;

    /**
     * 存储方式ID
     */
    private Long storageId;

    /**
     * 资源类型
     */
    private String resourceType;

    /**
     * 资源名称
     */
    private String resourceName;

    /**
     * 资源类型
     */
    private String mediaType;

    /**
     * 资源大小(字节)
     */
    private Long size;

    /**
     * 文件扩展名
     */
    private String fileExtension;

    /**
     * 上传用户的ID
     */
    private Long uploadUserId;

    /**
     * 父级资源的ID
     */
    private Long parentResourceId;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 更新时间
     */
    private Date updateTime;

    /**
     * 原始名称
     */
    private String originalName;

    /**
     * 资源路径
     */
    private String path;

    /**
     * 转Vo
     */
    public ResourceVo toVo() {
        ResourceVo vo = new ResourceVo();
        vo.setResourceId(resourceId);
        vo.setStorageId(storageId);
        vo.setResourceType(resourceType);
        vo.setResourceName(resourceName);
        vo.setMediaType(mediaType);
        vo.setSize(size);
        vo.setFileExtension(fileExtension);
        vo.setUploadUserId(uploadUserId);
        vo.setParentResourceId(parentResourceId);
        vo.setCreateTime(createTime);
        vo.setUpdateTime(updateTime);
        vo.setOriginalName(originalName);

        if (Objects.equals(resourceType, ResourceTypeEnum.FILE.getType())) {
            ResourceService resourceService =  SpringUtils.getBean(ResourceService.class);
            // 插入加密密文
            vo.setEncryptionId(resourceService.getEncryptionId(resourceId));
        }

        return vo;
    }
}